import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sports_new/controller/match/list/match_page_logic.dart';
import 'package:sports_new/controller/service/config_service.dart';
import 'package:sports_new/resources/colours.dart';
import 'package:sports_new/resources/styles.dart';
import 'package:sports_new/utils/app_config.dart';
import 'package:sports_new/utils/mode_config.dart';
import 'package:sports_new/utils/routes.dart';
import 'package:sports_new/utils/utils.dart';
import 'package:sports_new/utils/widget_utils.dart';
import 'package:sports_new/widgets/bottom_picker_widget.dart';
import 'package:sports_new/widgets/base/qkd_widget.dart';
import 'package:visibility_detector/visibility_detector.dart';

import '../../../widgets/base/qkd_text.dart';

class LqConfigPage extends StatefulWidget {
  const LqConfigPage({super.key});

  @override
  State<LqConfigPage> createState() => _LqConfigPageState();
}

class _LqConfigPageState extends State<LqConfigPage> {
  ConfigService configService = Get.find<ConfigService>();
  LqConfig config = Get.find<ConfigService>().lqConfig;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: Styles.appBar(title: QkdText('篮球比赛设置')),
      backgroundColor: Colours.scaffoldBg,
      body: VisibilityDetector(
        key: const Key('LqConfigPage'),
        onVisibilityChanged: (info) {
          if (!info.visibleBounds.isEmpty) {
            update();
          }
        },
        child: Padding(
          padding: const EdgeInsets.symmetric(horizontal: 8),
          child: ListView(
            children: [
              separateTitle('应用内提醒'),
              QkdWidget.cell(
                  '提醒范围', config.lqAlert1 == 0 ? '我关注的比赛' : '全部比赛',
                  onTap: () async {
                Utils.onEvent('lqpd_szan', params: {'lqpd_szan': '0'});
                // Get.toNamed(Routes.alertRangeConfig, arguments: 1);
                final result = await Get.bottomSheet(BottomPickerWidget(
                  ['我关注的比赛', '全部比赛'],
                  title: '提醒范围',
                  initialIndex: config.lqAlert1,
                ));
                // final result = await Get.dialog(
                //     CommonDialog.select('提醒范围', selectList: ['我关注的比赛', '全部比赛']));
                if (result != null) {
                  if (config.lqAlert1 == result) {
                    return;
                  }
                  config.lqAlert1 = result;
                  update();
                  Get.find<ConfigService>()
                      .update(ConfigType.lqAlert1, config.lqAlert1);
                }
              }),
              const SizedBox(height: 10),
              jinqiu(),
              if (config.lqAlert2.contains(0)) ...[
                const SizedBox(height: 10),
                cell([
                  title('弹窗范围'),
                  const Spacer(),
                  selectItem('比赛列表', config.lqAlert3 == 0, onTap: () {
                    Utils.onEvent('lqpd_szan', params: {'lqpd_szan': '2'});
                    if (config.lqAlert3 == 0) return;
                    config.lqAlert3 = 0;
                    update();
                    configService.update(ConfigType.lqAlert3, config.lqAlert3);
                  }),
                  const SizedBox(width: 13),
                  selectItem('APP全局', config.lqAlert3 == 1, onTap: () {
                    Utils.onEvent('lqpd_szan', params: {'lqpd_szan': '2'});
                    if (config.lqAlert3 == 1) return;
                    config.lqAlert3 = 1;
                    update();
                    configService.update(ConfigType.lqAlert3, config.lqAlert3);
                  }),
                ]),
              ],
              const SizedBox(height: 10),
              Styles.card(
                child: switchCell('全部、赛程、赛果频道共享筛选条件', config.lqAlert4 == 1,
                    ConfigType.lqAlert4),
              ),
              separateTitle('列表数据显示设置'),
              zhishu(),
              separateTitle('通知设置'),
              QkdWidget.cell(
                  '关注比赛通知',
                  config.lqPush1 == 1 && configService.pushConfig.pushAll == 1
                      ? config.getPushConfigCount()
                      : '0/2', onTap: () {
                Utils.onEvent('lqpd_szan', params: {'lqpd_szan': '7'});
                Get.toNamed(Routes.lqPushConfig);
              }),
              const SizedBox(height: 50),
            ],
          ),
        ),
      ),
    );
  }

  Widget cell(List<Widget> children, {Function()? onTap}) {
    return GestureDetector(
      onTap: onTap,
      behavior: HitTestBehavior.opaque,
      child: Styles.card(
        height: 52,
        color: Colours.white,
        padding: const EdgeInsets.symmetric(horizontal: 16),
        child: Row(children: children),
      ),
    );
  }

  Widget title(String title) {
    return QkdText(title, fontSize: 16);
  }

  Widget separateTitle(String title) {
    return Padding(
      padding: const EdgeInsets.only(left: 8, top: 12, bottom: 6),
      child: QkdText(title, color: Colours.grey66, fontSize: 13),
    );
  }

  Widget detail(String title) {
    return QkdText(title, color: Colours.grey66, fontSize: 16);
  }

  Widget switchCell(String title, bool select, int id) {
    return Styles.card(
      height: 52,
      color: Colours.white,
      padding: const EdgeInsets.only(left: 12, right: 10),
      shadow: false,
      child: Row(children: [
        QkdText(
          title,
          color: Colours.text, fontSize: 16,
        ),
        const Spacer(),
        Transform.scale(
          scale: 0.9,
          child: CupertinoSwitch(
            value: select,
            // trackColor: Colours.blue2766D6,
            activeColor: Colours.main,
            onChanged: (value) {
              if (id == ConfigType.lqAlert4) {
                Utils.onEvent('lqpd_szan', params: {'lqpd_szan': '3'});
                config.lqAlert4 = (config.lqAlert4 == 1 ? 0 : 1);
                configService.update(id, config.lqAlert4);
              } else if (id == ConfigType.lqList2) {
                Utils.onEvent('lqpd_szan', params: {'lqpd_szan': '5'});
                config.lqList2 = (config.lqList2 == 1 ? 0 : 1);
                configService.update(id, config.lqList2);
              } else if (id == ConfigType.lqList3) {
                Utils.onEvent('lqpd_szan', params: {'lqpd_szan': '6'});
                config.lqList3 = (config.lqList3 == 1 ? 0 : 1);
                configService.update(id, config.lqList3);
              }
              if (id != ConfigType.lqAlert4) {
                final matchController =
                    Get.find<MatchPageLogic>().getMatchController();
                matchController.updateView();
              }
              update();
            },
          ),
        )
      ]),
    );
  }

  Widget selectItem(String title, bool select, {Function()? onTap}) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: onTap,
      child: Container(
        height: 40,
        child: Row(
          children: [
            Image.asset(
              Utils.getImgPath(select ? 'config_check.png' : 'config_uncheck.png'),
              width: 22,
              height: 22,
            ),
            // const SizedBox(width: 3),
            QkdText(
              title,
              fontSize: 14, color: Colours.grey66,
            )
          ],
        ),
      ),
    );
  }

  jinqiu() {
    return cell([
      title('赛果提醒'),
      const Spacer(),
      selectItem('弹窗', config.lqAlert2.contains(0), onTap: () {
        Utils.onEvent('lqpd_szan', params: {'lqpd_szan': '1'});
        if (config.lqAlert2.contains(0)) {
          config.lqAlert2.clear();
        } else {
          config.lqAlert2.add(0);
        }
        update();
        configService.update(ConfigType.lqAlert2, config.lqAlert2);
      }),
      const SizedBox(width: 12),
      selectItem('声音', config.lqAlert2.contains(1), onTap: () {
        Utils.onEvent('lqpd_szan', params: {'lqpd_szan': '1'});
        if (config.lqAlert2.contains(1)) {
          config.lqAlert2.remove(1);
        } else {
          config.lqAlert2.add(1);
          config.lqAlert2.addIf(!config.lqAlert2.contains(0), 0);
        }
        update();
        configService.update(ConfigType.lqAlert2, config.lqAlert2);
      }),
      const SizedBox(width: 12),
      selectItem('震动', config.lqAlert2.contains(2), onTap: () {
        Utils.onEvent('lqpd_szan', params: {'lqpd_szan': '1'});
        if (config.lqAlert2.contains(2)) {
          config.lqAlert2.remove(2);
        } else {
          config.lqAlert2.add(2);
          config.lqAlert2.addIf(!config.lqAlert2.contains(0), 0);
        }
        update();
        configService.update(ConfigType.lqAlert2, config.lqAlert2);
      }),
    ]);
  }

  zhishu() {
    return Styles.card(
      color: Colours.white,
      child: Column(children: [
        // if(AppConfig.config.showOdds && ModeConfig.matchListOdds)...[
        //   QkdWidget.cell('指数', config.lqList1 == 1 ? '开启' : '关闭',
        //     isCard: false, onTap: () {
        //       Get.toNamed(Routes.lqOddsConfig);
        //     }),
        //   const Divider(height: 0.5, color: Colours.greyEE, indent: 16)
        // ],
        switchCell('球队排名', config.lqList2 == 1, ConfigType.lqList2),
        const Divider(height: 0.5, color: Colours.greyEE, indent: 16),
        switchCell('彩种编号', config.lqList3 == 1, ConfigType.lqList3),
      ]),
    );
  }
}
